import pytest
import requests


@pytest.fixture
def base_url():
    return "http://127.0.0.1:8080"


def test_add_region_success(base_url):
    url = f"{base_url}/region"
    cookies = {"id": "2"}
    data = {
        "name": "Test Region",
        "parentRegion": "Parent Region",
        "regionType": 1,
        "latitude": 123.456,
        "longitude": 789.012,
        "latitude1": 123.456,
        "longitude1": 789.012,
    }
    response = requests.post(url, json=data, cookies=cookies)
    assert response.status_code == 201
    assert "id" in response.json()
    assert response.json()["name"] == data["name"]
    assert response.json()["parentRegion"] == data["parentRegion"]
    assert response.json()["regionType"] == data["regionType"]
    assert response.json()["latitude"] == data["latitude"]
    assert response.json()["longitude"] == data["longitude"]


def test_add_region_missing_fields(base_url):
    url = f"{base_url}/region"
    cookies = {"id": "2"}
    data = {"parentRegion": "Parent Region", "regionType": 1}
    response = requests.post(url, json=data, cookies=cookies)
    assert response.status_code == 400


def test_add_region_unauthorized(base_url):
    url = f"{base_url}/region"
    data = {
        "name": "Test Region",
        "parentRegion": "Parent Region",
        "regionType": 1,
        "latitude": 123.456,
        "longitude": 789.012,
    }
    response = requests.post(url, json=data)
    assert response.status_code == 401


def test_add_region_duplicate_coordinates(base_url):
    url = f"{base_url}/region"
    cookies = {"id": "2"}
    data = {
        "name": "Test Region",
        "parentRegion": "Parent Region",
        "regionType": 1,
        "latitude": 123.456,
        "longitude": 789.012,
        "latitude1": 123.456,
        "longitude1": 789.012,
    }

    response = requests.post(url, json=data, cookies=cookies)
    assert response.status_code == 409


def test_get_region_success(base_url):
    region_id = 1
    url = f"{base_url}/region/{region_id}"
    cookies = {"id": "2"}
    response = requests.get(url, cookies=cookies)
    assert response.status_code == 200
    assert "id" in response.json()
    assert "regionType" in response.json()
    assert "accountId" in response.json()
    assert "name" in response.json()
    assert "parentRegion" in response.json()
    assert "latitude" in response.json()
    assert "longitude" in response.json()


def test_get_region_missing_id(base_url):
    url = f"{base_url}/region/{None}"
    cookies = {"id": "2"}
    response = requests.get(url, cookies=cookies)
    assert response.status_code == 400


def test_get_region_invalid_id(base_url):
    region_id = -1
    url = f"{base_url}/region/{region_id}"
    cookies = {"id": "2"}
    response = requests.get(url, cookies=cookies)
    assert response.status_code == 400


def test_get_region_unauthorized(base_url):
    region_id = 1
    url = f"{base_url}/region/{region_id}"
    response = requests.get(url)
    assert response.status_code == 401


def test_get_region_not_found(base_url):
    region_id = 9999
    url = f"{base_url}/region/{region_id}"
    cookies = {"id": "2"}
    response = requests.get(url, cookies=cookies)
    assert response.status_code == 404


def test_update_region_success(base_url):
    region_id = 1
    url = f"{base_url}/region/{region_id}"
    cookies = {"id": "2"}
    data = {
        "name": "Updated Region Name",
        "parentRegion": "Updated Parent Region Name",
        "regionType": 1,
        "latitude": 12.345,
        "longitude": 67.890,
        "latitude1": 123.456,
        "longitude1": 789.012,
    }
    response = requests.put(url, json=data, cookies=cookies)
    assert response.status_code == 200
    assert "id" in response.json()
    assert response.json()["name"] == data["name"]
    assert response.json()["parentRegion"] == data["parentRegion"]
    assert response.json()["latitude"] == data["latitude"]
    assert response.json()["longitude"] == data["longitude"]


def test_update_region_missing_fields(base_url):
    region_id = 1
    url = f"{base_url}/region/{region_id}"
    cookies = {"id": "2"}
    data = {"parentRegion": "Updated Parent Region Name"}
    response = requests.put(url, json=data, cookies=cookies)
    assert response.status_code == 400


def test_update_region_unauthorized(base_url):
    region_id = 1
    url = f"{base_url}/region/{region_id}"
    data = {
        "name": "Updated Region Name",
        "parentRegion": "Updated Parent Region Name",
        "regionType": 1,
        "latitude": 12.345,
        "longitude": 67.890,
    }
    response = requests.put(url, json=data)
    assert response.status_code == 401


def test_update_region_not_found(base_url):
    region_id = 9999
    url = f"{base_url}/region/{region_id}"
    cookies = {"id": "2"}
    data = {
        "name": "Updated Region Name",
        "parentRegion": "Updated Parent Region Name",
        "regionType": 2,
        "latitude": 12.345,
        "longitude": 67.890,
        "latitude1": 132.345,
        "longitude1": 637.890,
    }
    response = requests.put(url, json=data, cookies=cookies)
    assert response.status_code == 404


def test_update_region_duplicate_coordinates(base_url):
    region_id = 1
    url = f"{base_url}/region/{region_id}"
    cookies = {"id": "2"}
    data = {
        "name": "Updated Region Name",
        "parentRegion": "Updated Parent Region Name",
        "regionType": 1,
        "latitude": 12.345,
        "longitude": 67.890,
        "latitude1": 122.345,
        "longitude1": 673.890,
    }
    response = requests.put(url, json=data, cookies=cookies)
    assert response.status_code == 409


def test_delete_region_success(base_url):
    region_id = 1
    url = f"{base_url}/region/{region_id}"
    cookies = {"id": "2"}
    response = requests.delete(url, cookies=cookies)
    assert response.status_code == 200


def test_delete_region_missing_id(base_url):
    url = f"{base_url}/region/{None}"
    cookies = {"id": "2"}
    response = requests.delete(url, cookies=cookies)
    assert response.status_code == 400


def test_delete_region_unauthorized(base_url):
    region_id = 1
    url = f"{base_url}/region/{region_id}"
    response = requests.delete(url)
    assert response.status_code == 401


def test_delete_region_not_found(base_url):
    region_id = 9999
    url = f"{base_url}/region/{region_id}"
    cookies = {"id": "2"}
    response = requests.delete(url, cookies=cookies)
    assert response.status_code == 404
